package com.shouer.doc.ui.ordermanager.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.shouer.doc.R;
import com.shouer.doc.common.ShouerConst;
import com.shouer.doc.common.util.AppUtil;
import com.shouer.doc.common.util.DateFormatUtil;
import com.shouer.doc.common.util.RelativeDateFormatUtil;
import com.shouer.doc.ui.neworder.model.SimpleOrderModel;
import com.shouer.doc.ui.ordermanager.view.IOrderManagerView;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by YYY-Work on 2017/4/20.
 */

public class InOrderAdapter extends RecyclerView.Adapter<OrderManagerVH> implements View.OnClickListener{

    private Context mContext;
    private IOrderManagerView managerView;
    private LayoutInflater mInflater;
    private List<SimpleOrderModel> mOrderModels;

    public InOrderAdapter(Context context, IOrderManagerView managerView) {
        this.mContext = context;
        this.managerView = managerView;
        mInflater = LayoutInflater.from(context);

    }

    @Override
    public OrderManagerVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.fg_ordermanager_item_layout, null);
        return new OrderManagerVH(itemView);
    }

    @Override
    public void onBindViewHolder(OrderManagerVH holder, int position) {
        Log.i(InOrderAdapter.class.getSimpleName(),"onBindViewHolder:"+position);
        SimpleOrderModel orderModel = mOrderModels.get(position);
        setOrderIco(holder.orderIcoView,orderModel);

        /**
         * 未读消息信息
         * */
        if (orderModel.getUnReadCount() > 0) {
            holder.unReadCountView.setText(String.valueOf(orderModel.getUnReadCount()));
            holder.unReadCountView.setVisibility(View.VISIBLE);
        } else {
            holder.unReadCountView.setVisibility(View.GONE);
        }

        if (!orderModel.isHasGotUnReadCount()) {
            orderModel.setHasGotUnReadCount(true);
            managerView.refreshUnReadCount(orderModel);
        }

        try {
            holder.timeView.setText(RelativeDateFormatUtil.format(DateFormatUtil.stringToDate(orderModel.getCreateTime().replace("T", "\b"), "yyyy-MM-dd\bHH:mm:ss")));
        } catch (Exception e) {
            Log.d(InOrderAdapter.class.getSimpleName(), e.toString());
            holder.timeView.setText("");
        }

        if (orderModel.getOrderType() == ShouerConst.ORDER_TYPE_YAO) {
            holder.userNameView.setText(orderModel.getPatient().getPatName());
            holder.userAgeView.setText("");
            holder.userGenderView.setText("");

            holder.orderPreLayout.setVisibility(View.GONE);
            holder.descriptionView.setVisibility(View.GONE);
        }else
        {
            holder.userNameView.setText(orderModel.getPatient().getPatName());
            int age = 0;
            try {
                age = DateFormatUtil.getAgeByBirthday(DateFormatUtil.stringToDate(orderModel.getPatient().getPatBirthday().replace("T", "\b"), "yyyy-MM-dd"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            age = age < 0 ? 0 : age;
            holder.userAgeView.setText(mContext.getResources().getString(R.string.patient_age, age));
            holder.userGenderView.setText(orderModel.getPatient().getPatSex() == 0 ? "女" : "男");

            if (orderModel.getOrderType() == ShouerConst.ORDER_TYPE_TUWEN) {
                holder.descriptionView.setText(orderModel.getPatDescribe());
                holder.orderPreLayout.setVisibility(View.GONE);
                holder.descriptionView.setVisibility(View.VISIBLE);
            } else {
                try {
                    Date createDate = DateFormatUtil.stringToDate(orderModel.getScheduleTime().replace("T", "\b"), "yyyy-MM-dd\bHH:mm:ss");
                    holder.orderTimeView.setText(DateFormatUtil.dateToString(createDate, "yyyy-MM-dd HH:mm"));
                } catch (ParseException e) {
                    Log.e(OrderInOrderListAdapter.class.getSimpleName(), e.toString());
                    holder.orderTimeView.setText("-");
                }
                holder.durationView.setText(mContext.getResources().getString(R.string.order_duration, orderModel.getOrderTime()));
                if (orderModel.getOrderType() == ShouerConst.ORDER_TYPE_DIANHUA) {
                    holder.orderTelView.setText(orderModel.getContactTel());
                    holder.telLayout.setVisibility(View.VISIBLE);
                } else {
                    holder.telLayout.setVisibility(View.GONE);
                }
                holder.descriptionView.setVisibility(View.GONE);
                holder.orderPreLayout.setVisibility(View.VISIBLE);
            }
        }


        holder.itemView.setTag(R.id.list_position, position);
        holder.itemView.setOnClickListener(this);



    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    public void refreshDocList(List<SimpleOrderModel> orderModels) {
        this.mOrderModels = orderModels;
        notifyDataSetChanged();
    }

    public void attachToDocList(List<SimpleOrderModel> orderModels) {
        if (null != orderModels) {
            if (null == mOrderModels) {
                mOrderModels = new ArrayList<>(0);
            }

            mOrderModels.addAll(orderModels);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        if (null == mOrderModels) {
            return 0;
        } else {
            return mOrderModels.size();
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.orderinorder_item:
                int orderInorderPosition = (int) view.getTag(R.id.list_position);
                SimpleOrderModel orderModel = mOrderModels.get(orderInorderPosition);
                managerView.onOrderInOrderItemClick(orderModel);
                break;
        }
    }

    public SimpleOrderModel getOrderByOrderId(String orderId) {
        if (null == mOrderModels) {
            return null;
        }
        for (SimpleOrderModel simpleOrderModel : mOrderModels) {
            if (simpleOrderModel.getOrderId().equals(orderId)) {
                return simpleOrderModel;
            }
        }
        return null;
    }

    public void removeOrderById(String orderId) {
        if (null == mOrderModels) {
            return;
        }
        for (SimpleOrderModel simpleOrderModel : mOrderModels) {
            if (simpleOrderModel.getOrderId().equals(orderId)) {
                mOrderModels.remove(simpleOrderModel);
                notifyDataSetChanged();
                break;
            }
        }
    }

    public void setReQueryCount() {
        if (null != mOrderModels) {
            for (SimpleOrderModel orderModel : mOrderModels) {
                orderModel.setHasGotUnReadCount(false);
            }
        }
    }


    public void refreshOrdItem(SimpleOrderModel orderModel) {
        if (null != mOrderModels) {
            int position = (mOrderModels.indexOf(orderModel)+1);
            if (position>0)
            {
                try {
                    notifyItemChanged(position);
                }catch (Exception e)
                {
                    Log.e(InOrderAdapter.class.getSimpleName(),e.getMessage());
                }
            }
        }
    }

    public void refreshOrdCount(String orderId,int count)
    {
        Log.i(InOrderAdapter.class.getSimpleName(),"orderId-------"+orderId+"------count------"+count);
        if (null!=mOrderModels)
        {
            for (int i=0;i<mOrderModels.size();i++)
            {
                if (mOrderModels.get(i).getOrderId().equals(orderId))
                {
                    mOrderModels.get(i).setUnReadCount(count);
                    try {
                        notifyItemChanged((count+1));
                    }catch (Exception e)
                    {
                        Log.e(InOrderAdapter.class.getSimpleName(),e.getMessage());
                    }
                }
            }
        }
    }


    private void setOrderIco(SimpleDraweeView orderIco, SimpleOrderModel orderModel) {
        String icoUriString = null;
        switch (orderModel.getChannel()) {
            case 1:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_mfzx;
                break;
            case 2:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_zjzx;
                break;
            case 3:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_audio;
                break;
            case 4:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_ghsy;
                break;
            case 6:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_video;
                break;
            case 7:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_quickask;
                break;
            case 8:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_ghsy;
                break;
            case 9:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_jkbg;
                break;
        }
        Uri uri = Uri.parse(icoUriString);
        orderIco.setImageURI(uri);

    }
}
